import {ProColumns, ProTable} from '@ant-design/pro-components';
import {message, Modal} from 'antd';
import React, {PropsWithChildren} from 'react';
import {updateLog} from "@/services/logService";

interface UpdateModalProps {
    oldData: LogType.Log;
    modalVisible: boolean;
    columns: ProColumns<LogType.Log>[];
    onSubmit: () => void;
    onCancel: () => void;
}



/**
 * 更新数据模态框
 * @param props
 * @constructor
 */
const UpdateModal: React.FC<PropsWithChildren<UpdateModalProps>> = (props) => {
    const {oldData, columns, modalVisible, onSubmit, onCancel} = props;

    /**
     * 更新数据模态框
     * @param fields
     */
    const handleUpdate = async (fields: LogType.LogUpdateRequest) => {
        const hide = message.loading('正在更新');
        await updateLog(fields).then(res => {
            message.success('更新成功');
            hide();
            onCancel();
            return true;
        }).catch(e => {
            message.error('更新失败请重试！'+ e.message);
            hide();
            return false;
        })
        return false;
    };

    return (
        <Modal
            destroyOnClose
            title="更新"
            visible={modalVisible}
            onCancel={() => onCancel()}
            footer={null}
        >
            <ProTable<LogType.Log, LogType.Log>
                onSubmit={async (value) => {
                    const success = await handleUpdate({
                        ...value,
                        id: oldData.id,
                    });
                    if (success) {
                        onCancel();
                    }
                }}
                rowKey="id"
                type="form"
                form={{
                    initialValues: oldData,
                }}
                columns={columns}
            />
        </Modal>
    );
};

export default UpdateModal;
